package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.XueshengEntity;
import com.entity.ZuoyebuzhiEntity;
import com.service.KechengxinxiService;
import com.service.XueshengService;
import com.service.ZuoyebuzhiService;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/xueshengxuanke3")
public class Xueshengxuanke3Controller {

    @Autowired
    private ZuoyebuzhiService zuoyebuzhiService;

    @Autowired
    private XueshengService xueshengService;

    /**
     * 查询课题列表
     */
    @GetMapping("/list/{courseName}")
    public R ketiList(@PathVariable String courseName){
        List<ZuoyebuzhiEntity> kechengmingchengList = zuoyebuzhiService.selectList(new EntityWrapper<ZuoyebuzhiEntity>()
                .eq("kechengmingcheng", courseName)
                .eq("status", "已发布"));
        return R.ok().put("data", kechengmingchengList);
    }

    /**
     * 查询该课题是否被选择
     */
    @GetMapping("/get/{ketiId}")
    public R get(@PathVariable String ketiId, HttpServletRequest request){
        String xuehao = (String) request.getSession().getAttribute("username");
        ZuoyebuzhiEntity zuoyebuzhiEntity = zuoyebuzhiService.selectById(ketiId);
        String xuehao2 = zuoyebuzhiEntity.getXuehao();
        String res;
        if(xuehao2 == null || xuehao2.isEmpty()) res = "可选";
        else if(xuehao.equals(xuehao2)) res = "已选";
        else res = "不可选";
        return R.ok().put("data", res);
    }

    /**
     * 选择课题
     */
    @PostMapping("/select/{ketiId}")
    public R selectKeti(@PathVariable String ketiId, HttpServletRequest request) {
        String xuehao = (String) request.getSession().getAttribute("username");
        String xueshengxingming = xueshengService.selectOne(new EntityWrapper<XueshengEntity>()
                .eq("xuehao", xuehao)).getXueshengxingming();
        String kechengmingcheng = zuoyebuzhiService.selectById(ketiId).getKechengmingcheng();

        // 检查该学生是否已经选择了其他课题
        List<ZuoyebuzhiEntity> existingSelection = zuoyebuzhiService.selectList(
            new EntityWrapper<ZuoyebuzhiEntity>()
                .eq("xuehao", xuehao)
                    .eq("kechengmingcheng", kechengmingcheng)
        );
        if (!existingSelection.isEmpty()) {
            return R.error("您已经选择了其他课题，不能重复选择");
        }

        // 检查该课题是否已被其他学生选择
        ZuoyebuzhiEntity keti = zuoyebuzhiService.selectById(ketiId);
        if (keti == null) {
            return R.error("课题不存在");
        }
        if (keti.getXuehao() != null && !keti.getXuehao().isEmpty()) {
            return R.error("该课题已被其他学生选择");
        }

        // 更新课题信息
        keti.setXuehao(xuehao);
        keti.setXueshengxingming(xueshengxingming);
        zuoyebuzhiService.updateById(keti);

        return R.ok("选择课题成功");
    }

    /**
     * 取消选择课题
     */
    @PostMapping("/cancel/{ketiId}")
    public R cancelKeti(@PathVariable String ketiId, HttpServletRequest request) {
        String xuehao = (String) request.getSession().getAttribute("username");
        
        // 检查课题是否存在且是否是该学生选择的
        ZuoyebuzhiEntity keti = zuoyebuzhiService.selectById(ketiId);
        if (keti == null) {
            return R.error("课题不存在");
        }
        if (!xuehao.equals(keti.getXuehao())) {
            return R.error("您没有选择该课题，无法取消");
        }

        // 清空学生信息
        zuoyebuzhiService.cancelById(ketiId);

        return R.ok("取消选择成功");
    }
}
